import 'package:flutter/material.dart';
import 'package:flutter_hx/model/address.dart';
import 'package:flutter_hx/services/app_theme_services.dart';
import 'package:flutter_hx/theme/font_size.dart';
import 'package:flutter_hx/widgets/ez_image.dart';

class AddressItem extends StatelessWidget {
  const AddressItem.managing({
    Key? key,
    this.isFirst,
    this.isLast,
    required this.data,
    this.onTap,
    this.nameStyle,
    this.phoneStyle,
    this.defaultIconColor,
    this.addressStyle,
    this.onSetDefault,
    this.onDelete,
  })  : isCommonItem = false,
        super(key: key);

  const AddressItem.common(
      {Key? key,
      this.isFirst,
      this.isLast,
      required this.data,
      this.onTap,
      this.nameStyle,
      this.phoneStyle,
      this.defaultIconColor,
      this.addressStyle})
      : onSetDefault = null,
        onDelete = null,
        isCommonItem = true,
        super(key: key);

  final bool? isFirst;
  final bool? isLast;
  final Address data;
  final Function? onTap;
  final Function? onSetDefault;
  final Function? onDelete;
  final TextStyle? nameStyle;
  final TextStyle? phoneStyle;
  final TextStyle? addressStyle;
  final Color? defaultIconColor;
  final bool isCommonItem;

  @override
  Widget build(BuildContext context) {
    return isCommonItem ? _commonItem() : _managingItem();
  }

  ///非管理状态的item
  Widget _commonItem() {
    ///初始化首尾item
    EdgeInsets margin = const EdgeInsets.symmetric(horizontal: 10);
    EdgeInsets padding = const EdgeInsets.symmetric(horizontal: 15);
    BorderRadius radius = BorderRadius.only(
      topLeft: (isFirst ?? false) ? const Radius.circular(7.5) : Radius.zero,
      topRight: (isFirst ?? false) ? const Radius.circular(7.5) : Radius.zero,
      bottomLeft: (isLast ?? false) ? const Radius.circular(7.5) : Radius.zero,
      bottomRight: (isLast ?? false) ? const Radius.circular(7.5) : Radius.zero,
    );
    if (isFirst ?? false) {
      margin = margin.copyWith(top: 9);
      padding = padding.copyWith(top: 15);
      // radius.add(const BorderRadius.only(
      //   topLeft: Radius.circular(7.5),
      //   topRight: Radius.circular(7.5),)
      // );
    }
    if (isLast ?? false) {
      margin = margin.copyWith(bottom: 72);
      padding = padding.copyWith(bottom: 15);
      // radius.add(const BorderRadius.only(
      //   bottomLeft: Radius.circular(7.5),
      //   bottomRight: Radius.circular(7.5),
      // ));
    }
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: radius,
      ),
      padding: padding,
      margin: margin,
      child: GestureDetector(
          onTap: () => onTap?.call(),
          behavior: HitTestBehavior.opaque,
          child: AddressItemInner(
            data: data,
          )),
    );
  }

  ///管理状态的item
  Widget _managingItem() {
    EdgeInsets margin = const EdgeInsets.symmetric(horizontal: 10);
    if (isFirst ?? false) {
      margin = margin.copyWith(top: 9);
    }
    if (isLast ?? false) {
      margin = margin.copyWith(bottom: 72);
    }
    return Container(
      padding: const EdgeInsets.all(15).copyWith(bottom: 10),
      margin: margin,
      decoration: BoxDecoration(
          color: Colors.white, borderRadius: BorderRadius.circular(7.5)),
      child: Column(
        children: [
          ///上部分
          GestureDetector(
              onTap: () => onTap?.call(),
              behavior: HitTestBehavior.opaque,
              child: AddressItemInner(
                data: data,
              )),

          ///分界线
          Container(
            margin: const EdgeInsets.only(top: 15, bottom: 10),
            height: 0.5,
            color: const Color(0xFFF2F2F2),
          ),

          ///下部分 默认地址勾选 删除
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              ///去除默认
              // GestureDetector(
              //     onTap: () => onSetDefault?.call(),
              //     behavior: HitTestBehavior.opaque,
              //     child: _defaultButton(data.isDefault == "1")),
              const Expanded(child: SizedBox()),
              InkWell(
                onTap: () => onDelete?.call(),
                child: const Text("删除",
                    style: TextStyle(
                        fontSize: FontSize.pt12, color: Color(0xFF646566))),
              )
            ],
          )
        ],
      ),
    );
  }

  ///管理模式下默认地址勾选
  Row _defaultButton(bool isDefault) {
    return Row(
      children: [
        if (isDefault)
          const EzImage.asset(
            "icon_check_select.png",
            height: 14,
            width: 14,
          )
        else
          Container(
            height: 14,
            width: 14,
            decoration: BoxDecoration(
              border: Border.all(color: const Color(0xFF969799), width: 1),
              borderRadius: BorderRadius.circular(99),
            ),
          ),
        const SizedBox(
          width: 8,
        ),
        const Text(
          "默认地址",
          style: TextStyle(fontSize: FontSize.pt12, color: Color(0xFF646566)),
        ),
      ],
    );
  }
}

class AddressItemInner extends StatelessWidget {
  const AddressItemInner(
      {Key? key,
      required this.data,
      this.nameStyle,
      this.phoneStyle,
      this.addressStyle,
      this.defaultIconColor = const Color(0xFF00C3B3)})
      : super(key: key);
  final Address data;
  final TextStyle? nameStyle;
  final TextStyle? phoneStyle;
  final TextStyle? addressStyle;
  final Color defaultIconColor;

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        ///姓名，电话，默认icon
        firstRow(
            name: data.realName ?? "",
            phone: data.phone ?? "",
            isDefault: data.isDefault == "1"),
        const SizedBox(height: 3.5),

        ///地址，编辑图标
        ///data.area data.town????
        secondRow(
            addressStr:
                "${data.province} ${data.city} ${data.area ?? data.town} ${data.detailed}")
      ],
    );
  }

  ///第一行
  Row firstRow(
      {required String name, required String phone, required bool isDefault}) {
    return Row(
      children: [
        Text(
          name,
          style: nameStyle ??
              const TextStyle(fontSize: FontSize.pt16, color: Color(0xFF323233))
                  .bold(),
        ),
        const SizedBox(
          width: 5,
        ),
        Text(
          phone,
          style: phoneStyle ??
              const TextStyle(
                  fontSize: FontSize.pt12, color: Color(0xFF969799)),
        ),
        const SizedBox(
          width: 12,
        ),
        ///去除默认
        // if (isDefault)
        //   Container(
        //     padding: const EdgeInsets.symmetric(vertical: 2, horizontal: 4),
        //     decoration: BoxDecoration(
        //       borderRadius: BorderRadius.circular(2.5),
        //       border: Border.all(color: defaultIconColor, width: 0.5),
        //     ),
        //     child: Text("默认",
        //         style: TextStyle(color: defaultIconColor, fontSize: 11)),
        //   ),
      ],
    );
  }

  ///第二行
  Row secondRow({
    required String addressStr,
  }) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Expanded(
            child: Text(
          addressStr,
          style: addressStyle ??
              const TextStyle(
                  fontSize: FontSize.pt14, color: Color(0xFF333333)),
          maxLines: 2,
          overflow: TextOverflow.ellipsis,
        )),
        const SizedBox(
          width: 15,
        ),
        Padding(
          padding: const EdgeInsets.only(top: 3),
          child: SizedBox(
            width: 15,
            height: 15,
            child: Image.asset(
              "assets/images/icon_address_edit.png",
              fit: BoxFit.fill,
            ),
          ),
        )
      ],
    );
  }
}
